package com.ag.oa.core.controller;

import com.ag.oa.core.entity.vo.MessageVO;
import com.ag.oa.core.entity.vo.UserVO;
import com.ag.oa.core.service.IMessageService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by yxm on 21/10/2021.
 * 消息类控制器
 */
@RestController
@RequestMapping({"/message", "/test/message"})
public class MessageController {

    @Autowired
    private IMessageService messageService;

    private static final Logger logger = LoggerFactory.getLogger(MessageController.class);

    /**
     * 获取用户未读消息的数量
     */
    @RequestMapping(value = "/unread/count")
    public @ResponseBody String getUnreadCount(String openId) {

        JSONObject res = new JSONObject();
        try {
            messageService.getUserUnreadMessageCount(openId, res);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            res.put("resCode", 500);
        }
        return res.toJSONString();
    }

    /**
     * 获取用户消息
     * @param isRead 是否已读
     */
    @RequestMapping(value = "/messages")
    public @ResponseBody String getUnreadData(Integer userId, Integer isRead, Integer page, Integer pageSize) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            List<Map> messages = messageService.getUserAllMessagesData(userId, isRead, page, pageSize);
            res.put("messages", messages);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 保存提交会员申请
     */
    @RequestMapping(value = "/membership/apply")
    public @ResponseBody String apply2VipUser(Integer userId, String remark) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            messageService.saveNewOneMessageRecord(1, userId, null, null
                    , remark, 0);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 会员申请消息的处理
     */
    @RequestMapping(value = "/membership/handling")
    public @ResponseBody String handleMembershipMessage(Integer id, Integer userId, Integer status) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            messageService.updateHandleMembershipApplyMessage(id, userId, status);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 根据 id 获取消息
     */
    @RequestMapping(value = "/messageInfo")
    public @ResponseBody String getMessageInfo(Integer id) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            Map<String, Object> messageInfo = messageService.getMessageInfoById(id);
            res.put("messageInfo", messageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 根据任务 id 获取用户所有的任务评论的消息
     */
    @RequestMapping(value = "/task/forums", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getAllTaskForumMessageInfo(Integer taskId, Integer userId) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            messageService.getUserTaskForumMessages(taskId, userId, res);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 将消息标记为已读
     */
    @RequestMapping(value = "/hasRead")
    public @ResponseBody String update2Read(Integer id) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            messageService.updateMessageToRead(id);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    @PostMapping("/agreedCompleteTask")
    public String updateAgreedCompleteTask(@RequestBody MessageVO vo) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            Integer selectCount = messageService.updateAgreedCompleteTask(vo);
            // 0 表示，相关任务负责人已审批 或 当前分配的积分 已超出 任务总积分
            if (selectCount==0) {
                code = 1000;
            }
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    @GetMapping("/getMsgLastUpdateManager")
    public UserVO getMessageLastUpdateManagerInfo(Integer messageId) {

        UserVO vo = new UserVO();
        try {
            vo = messageService.getMessageLastUpdateManagerInfo(messageId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return vo;
    }

    /**
     * 同意/拒绝 合作社的邀请
     */
    @GetMapping ("/projectInvited/save")
    public String saveProjectInvited(Integer id, Integer projectId, Integer userId, Integer status) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            messageService.updateProjectInvitedData(id, projectId, userId, status);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

}
